package com.seaking.controller;

import com.seaking.pojo.Admin;
import com.seaking.service.DataService;
import com.seaking.service.LoginService;
import com.seaking.util.CommonJson;
import com.seaking.util.CommonUtil;
import com.seaking.util.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by wchha on 10/24 0024.
 */
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private DataService dataService;


    @RequestMapping(value = "/checkUserName", method = RequestMethod.GET)
    @ResponseBody
    public CommonJson checkUserName(String userName) {
        CommonJson json = new CommonJson();
        boolean ok = loginService.checkUserName(userName);
        if (ok) {
            json.setMsg("账号可注册！");
        } else {
            json.setSuccess(false);
            json.setMsg("账户已注册！");
        }
        return json;
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register() {
        return "register";
    }


    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(Admin admin, Model model) {
        try {
            loginService.addAdmin(admin);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", "注册失败！请重新注册");
            return "register";
        }
        model.addAttribute("msg", "注册成功！请登录");
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String loginDo(Model model, Admin admin, HttpSession httpSession) {
        if (CommonUtil.isNullOrEmpty(admin)
                || CommonUtil.isNullOrEmpty(admin.getPassWord())
                || CommonUtil.isNullOrEmpty(admin.getUserName())
                ) {
            model.addAttribute("msg", "输入信息有误");
            return "login";
        } else {
            admin.setPassWord(Md5Utils.getPwd(admin.getPassWord()));
            Admin admin1 = loginService.check(admin);
            if (admin1 != null) {
                Long counts = dataService.getCount();
                httpSession.setAttribute("userName", admin1.getUserName());
                httpSession.setAttribute("counts", counts);
                return "redirect:/index";
            }
            model.addAttribute("msg", "用户名或密码不正确！");
            return "login";
        }

    }
}
